package ndatabase

import (
	"fmt"
	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"wwp.space/NetSim/config"
)

var Db *gorm.DB

func init() {
	var err error
	config.HelloWorld()
	dsn := "%s:%s@@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local"
	dsn = fmt.Sprintf(dsn, viper.GetString("mysql.username"), viper.GetString("mysql.password"),
		viper.GetString("mysql.address"), viper.GetString("mysql.database"))
	// 调用 Open 方法，传入驱动名和连接字符串
	Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	// 检查是否有错误
	if err != nil {
		panic("连接数据库失败" + err.Error())
	}
}
